﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MathTest
{
    /// <summary>
    /// 实体管理窗体
    /// 题库采用系列化实体集方式保存到程序同级目录的mathbank.txt文件中
    /// 因为是序列化，所以mathbank.txt中若字符集不对，则无法查看,但程序能读
    /// 若要更新试题，现在只能采用删除之后，重新添加的方式
    /// </summary>
    public partial class AddExaminationForm : Form
    {
        #region 属性
        /// <summary>
        /// 实体集
        /// </summary>
        private List<Entity.MathOperation> mathList = new List<Entity.MathOperation>();
        /// <summary>
        /// 序列化实例
        /// </summary>
        private IFormatter formatter = new BinaryFormatter();
        #endregion

        #region 构造函数
        public AddExaminationForm()
        {
            InitializeComponent();
        }
        #endregion

        #region 事件
        private void btnSave_Click(object sender, EventArgs e)
        {
            lblSaveResult.Text = "";
            //定义运算数变量
            int firstOperand = 0;
            int secondOperand = 0;
            int result = 0;
            //检查基本的运算规则
            if (!int.TryParse(txtFirstOperand.Text, out firstOperand))
            {
                MessageBox.Show("第一个数格式不正确，请输入整数。", this.Text);
                return;
            }
            if (cmbOperator.Text == "")
            {
                MessageBox.Show("请选择运算符。", this.Text);
                return;
            }
            if (!int.TryParse(txtSecondOperand.Text, out secondOperand))
            {
                MessageBox.Show("第二个数结果格式不正确，请输入整数。", this.Text);
                return;
            }
            if (!int.TryParse(txtResults.Text, out result))
            {
                MessageBox.Show("结果格式不正确，请输入整数。", this.Text);
                return;
            }

            if (cmbOperator.Text == "/" && secondOperand == 0)
            {
                MessageBox.Show("除数不能为0。", this.Text);
                return;
            }

            try
            {
                if (File.Exists(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "mathbank.txt"))
                {
                    //读取实体类集

                    Stream readstream = new FileStream(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "mathbank.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
                    mathList = (List<Entity.MathOperation>)formatter.Deserialize(readstream);
                    readstream.Close();
                }

                //向实体类集添加新的试题实例

                mathList.Add(new Entity.MathOperation()
                {
                    firstOperand = firstOperand.ToString(),
                    signOperation = cmbOperator.Text,
                    secondOperand = secondOperand.ToString(),
                    results = result.ToString()
                });


                //将新添加的试题实例写入文档

                Stream writestream = new FileStream(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "mathbank.txt", FileMode.Create, FileAccess.Write, FileShare.None);
                formatter.Serialize(writestream, mathList);
                writestream.Close();
                lblSaveResult.Text = "第" + mathList.Count.ToString() + "个试题保存成功";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        #endregion
    }
}
